//
//  HTHttpRequestManager.m
//  TruckDriver
//
//  Created by Ting Han on 2017/10/19.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTHttpRequestManager.h"
#import "NetWorkCall.h"

@implementation HTHttpRequestManager

//检查新版本
+ (void)checkNewVersionSuccess:(nullable void (^)(id _Nullable responseObject))success failure:(nullable void (^)(NSError * _Nonnull error))failure{
    DBLog(@"检查新版本");
    [NetWorkCall getRequestWithURL:requestDriverversion param:nil success:^(id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
#pragma mark ---- 登陆
//请求验证码
+ (void)requestSecurityCodeWithPhoneNum:(NSString *)phoneNum success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSDictionary *params = @{@"phone" : phoneNum};
    if (phoneNum == nil || phoneNum.length <= 0) {
        if (failure) {
            NSError *error = nil;
            failure(error);
        }
        return;
    }
    [NetWorkCall getRequestWithURL:requestSecurityCode param:params success:^(id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
//登陆请求
+ (void)requestLoginWithPhoneNum:(NSString *)phoneNum withSecurityCode:(NSString *)code success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    if (phoneNum == nil || phoneNum.length == 0 || code == nil || code.length == 0) {
        if (failure) {
            NSError *error = nil;
            failure(error);
        }
        return;
    }
    NSDictionary *params = @{@"phone":phoneNum,@"yzm":code};
    [NetWorkCall getRequestWithURL:requestlogin param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
#pragma mark ---订阅线路
//获取订阅列表
+ (void)requestLinesListSuccess:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    if (token == nil || token.length <= 0) {
        return;
    }
    NSDictionary *params = @{@"token":token};
    DBLog(@"获取订阅列表：%@?token=%@",requestLinesList,token);
    [NetWorkCall getRequestWithURL:requestLinesList param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
        
    }];
}
//添加订阅线路
+ (void)requestCreatlinesWithFromAddress:(NSString *)fromAddress withToAddress:(NSString *)toAddress withLength:(NSString *)length withCartype:(NSString *)cartype withIssound:(BOOL)issound success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
   
    if (fromAddress == nil || fromAddress.length == 0 || toAddress == nil || toAddress.length == 0) {
        if (failure) {
            NSError *error = nil;
            failure(error);
        }
        return;
    }
    NSString *isStr = nil;
    if (issound) {
        isStr = @"1";
    }else{
        isStr = @"0";
    }
    NSDictionary *params = @{@"token":token,@"formaddr":fromAddress,@"toaddr":toAddress,@"length":length,@"cartype":cartype,@"issound":isStr};
    DBLog(@"添加订阅线路：%@?token=%@",requestCreatlines,token);
    [NetWorkCall getRequestWithURL:requestCreatlines param:params success:^(id responeObject) {
        
        if (success) {
            success(responeObject);
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}
//删除订阅线路
+ (void)requestDeletelinesWithId:(NSString *)withId success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{

    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token,@"id":withId};
    [NetWorkCall getRequestWithURL:requestDeletelines param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}
#pragma mark --- 货源搜索
//货源搜索
+ (void)requestGoodsSearchWithStarname:(NSString *)starname endname:(NSString *)endname length:(NSString *)length goodstype:(NSString *)goodstype cartype:(NSString *)cartype cargotype:(NSString *)cargotype page:(NSString *)page success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    token = (token == nil)?@"":token;
    NSDictionary *params = @{@"token":token
                             ,@"starname":starname
                             ,@"endname":endname
                             ,@"length":length
                             ,@"goodstype":goodstype
                             ,@"cartype":cartype
                             ,@"cargotype":cargotype
                             ,@"page":page
                             };
    [NetWorkCall getRequestWithURL:requestGoodsSearch param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}
//货源详情
+ (void)requestGoodsDetialWithOrderid:(NSString *)orderid success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token
                             ,@"orderid":orderid
                             };
    DBLog(@"货源详情：%@?token=%@&orderid=%@",requestGoodsDetial,token,orderid);
    [NetWorkCall getRequestWithURL:requestGoodsDetial param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
//司机接单
+ (void)requestChargeOrdersWithOrderid:(NSString *)orderid success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token
                             ,@"orderid":orderid
                             };
    DBLog(@"司机接单：%@?token=%@&orderid=%@",requestChargeOrders,token,orderid);
    [NetWorkCall getRequestWithURL:requestChargeOrders param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}
//货主资料
+ (void)requestCargosInforWithDriverid:(NSString *)driverid success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token
                             ,@"driverid":driverid
                             };
    DBLog(@"获取货主信息：%@?token=%@&driverid=%@",requestCargosInfor,token,driverid);
    [NetWorkCall getRequestWithURL:requestCargosInfor param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}
#pragma mark -- 注册
+ (void)requestUploadPictureWithType:(nullable NSString *)type image:(UIImage *_Nullable)image success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{

    
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token
                             ,@"type":type
                             };
    [NetWorkCall uploadImageWithArray:@[image] url:requestUploadUserspic fileName:@"img.jpg" names:@[@"image"] param:params progress:^(int64_t bytesProgress, int64_t totalProgress) {

    } success:^(id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.requestSerializer.timeoutInterval = 10;
//    [manager POST:requestUploadUserspic parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        NSData *imageData = UIImageJPEGRepresentation(image,0.5);
//        //上传的参数(上传图片，以文件流的格式)
//        [formData appendPartWithFileData:imageData
//                                    name:@"image"
//                                fileName:@"img.jpg"
//                                mimeType:@"image/jpeg"];
//    } progress:^(NSProgress * _Nonnull uploadProgress) {
//
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        if (success) {
//            success(responseObject);
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        if (failure) {
//            failure(error);
//        }
//    }];

}
+ (void)requestRegisterWithDrivercar:(NSString *)drivercar username:(NSString *)username cards:(NSString *)cards ismast:(NSString *)ismast cartype:(NSString *)cartype carnumber:(NSString *)carnumber carwm:(NSString *)carwm carlength:(NSString *)carlength goas:(NSString *)goas success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token
                             ,@"drivercar":drivercar
                             ,@"username":username
                             ,@"cards":cards
                             ,@"ismast":ismast
                             ,@"cartype":cartype
                             ,@"carnumber":carnumber
                             ,@"carwm":carwm
                             ,@"carlength":carlength
                             ,@"goas":goas
                             };
    DBLog(@"注册：%@?token=%@&drivercar=%@&username=%@&cards=%@&ismast=%@&cartype=%@&carnumber=%@&carwm=%@&carlength=%@&goas=%@",requestRegist,token,drivercar,username,cards,ismast,cartype,carnumber,carwm,carlength,goas);
    [NetWorkCall postRequestWithURL:requestRegist param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}
//变更车辆
+ (void)requestChangeCarWithCartype:(NSString *)cartype carnumber:(NSString *)carnumber carwm:(NSString *)carwm carlength:(NSString *)carlength goas:(NSString *)goas isfollow:(NSString *)isfollow success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token
                             ,@"isfollow":isfollow
                             ,@"cartype":cartype
                             ,@"carnumber":carnumber
                             ,@"carwm":carwm
                             ,@"carlength":carlength
                             ,@"goas":goas
                             };
    DBLog(@"更换车辆：%@?token=%@&isfollow=%@&cartype=%@&carnumber=%@&carwm=%@&carlength=%@&goas=%@",requestChangerCar,token,isfollow,cartype,carnumber,carwm,carlength,goas);
    [NetWorkCall getRequestWithURL:requestChangerCar param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}
//用户信息
+ (void)requestUserMessageSuccess:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token
                             };
    DBLog(@"个人信息：%@?token=%@",requestUserspic,token);
    [NetWorkCall getRequestWithURL:requestUserspic param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
#pragma mark -- 副驾管理
//副驾列表
+ (void)requestCopilotssuccess:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token
                             };
    DBLog(@"我的副驾：%@?token=%@",requestCopilots,token);
    [NetWorkCall getRequestWithURL:requestCopilots param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
        
    }];
}
//解绑
+ (void)requestUnbindCopilotsWithID:(NSString *)ID success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
     NSString *token = [HTMUserDefaultUtil readUserToken];
    DBLog(@"解绑：%@?token=%@&id=%@",requestUnbindCopilots,token,ID);
    NSDictionary *params = @{@"token":token,@"id":ID
                             };
    [NetWorkCall getRequestWithURL:requestUnbindCopilots param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
        
    }];
}
//绑定
+ (void)requestBindCopilotsWithPhone:(NSString *)phone success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token,@"phone":phone
                             };
    DBLog(@"绑定：%@?token=%@&phone=%@",requestBindCopilots,token,phone);
    [NetWorkCall getRequestWithURL:requestBindCopilots param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
        
    }];
}

+ (void)requestCopilotRejectApplyWithType:(NSString *)type driverid:(NSString *)driverid success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{
                             @"token":token
                             ,@"type":type
                             ,@"id":driverid
                             };
    DBLog(@"副驾拒绝主驾邀请：%@?token=%@&type=%@&id=%@",requestCopilotRejectApply,token,type,driverid);
    [NetWorkCall getRequestWithURL:requestCopilotRejectApply param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
        
    }];
}

+ (void)requestCopilotReturnApplyWithDriverid:(NSString *)driverid success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{
                             @"token":token
                             ,@"id":driverid
                             };
    DBLog(@"主驾撤销邀请：%@?token=%@&id=%@",requestCopilotReturnApply,token,driverid);
    [NetWorkCall getRequestWithURL:requestCopilotReturnApply param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
        
    }];
}
+ (void)requestCopilotApplyWithDriverid:(NSString *)driverid success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{
                             @"token":token
                             ,@"driverid":driverid
                             };
    DBLog(@"主驾同意申请：%@?token=%@&id=%@",requestCopilotApply,token,driverid);
    [NetWorkCall getRequestWithURL:requestCopilotApply param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
        
    }];
}

#pragma mark -- 运单
//订单列表
+ (void)requestWayBillsWithState:(NSString *)state success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token
                             ,@"state":state
                             };
    DBLog(@"运单列表：%@?token=%@&state=%@",requestMyWaybills,token,state);
    [NetWorkCall getRequestWithURL:requestMyWaybills param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}
//订单详情
+ (void)requestWayBillDetialWithOrderid:(NSString *)orderid success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token
                             ,@"orderid":orderid
                             };
    DBLog(@"订单详情：%@?token=%@&orderid=%@",requestMyWaybillDetial,token,orderid);
    [NetWorkCall getRequestWithURL:requestMyWaybillDetial param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}
//评价订单
+ (void)requestWayBillCommentWithOrderid:(NSString *)orderid scorll:(NSString *)scorll impress:(NSString *)impress success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token
                             ,@"orderid":orderid
                             ,@"scorll":scorll
                             ,@"impress":impress
                             };
    [NetWorkCall getRequestWithURL:requestCreateComment param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
//浏览记录
+ (void)requestRecordWithOrderid:(NSString *)orderid leav:(NSString *)leav type:(NSString *)type success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token
                             ,@"orderid":orderid
                             ,@"leav":@"0"
                             ,@"type":type
                             };
    DBLog(@"浏览记录:%@?token=%@&orderid=%@&leav=%@&type=%@",requestRecord,token,orderid,@"0",type);
    [NetWorkCall getRequestWithURL:requestRecord param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
    
}
#pragma mark --- 注册通知
+ (void)requestNotificationRegisterWithType:(NSString *)type regitid:(NSString *)regitid success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    if (token == nil || token.length <= 0) {
        if (failure) {
            NSError *error = nil;
            failure(error);
        }
        return;
    }
    NSDictionary *params = @{@"token":token
                             ,@"type":type
                             ,@"regitid":regitid
                             };
    [NetWorkCall getRequestWithURL:requestNotificationRegister param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}


+ (void)requestUserMessageListWithSuccess:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    NSDictionary *params = @{@"token":token
                             };
    DBLog(@"消息列表：%@?token=%@",requestUserMessage,token);
    [NetWorkCall getRequestWithURL:requestUserMessage param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
+ (void)requestUploadAddressWithAddress:(NSString *)address lat:(NSString *)lat lng:(NSString *)lng success:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nonnull))failure{
    NSString *token = [HTMUserDefaultUtil readUserToken];
    if (token == nil || token.length <= 0 ||address == nil || address.length <= 0 || lat == nil || lat.length <= 0 ||lng == nil || lng.length <= 0) {
        if (failure) {
            NSError *error = nil;
            failure(error);
        }
        return;
    }
    NSDictionary *params = @{@"token":token
                             ,@"address":address
                             ,@"lat":lat
                             ,@"lng":lng
                             };
    [NetWorkCall getRequestWithURL:requestPostAddress param:params success:^(id responeObject) {
        if (success) {
            success(responeObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
@end
